namespace CONC_DL {
﻿#include <stdio.h>
#include <pthread.h>
static pthread_mutex_t A, B;

void* printmsg1(void* msg) {
	pthread_mutex_lock(&A);  // Execution step 1
	pthread_mutex_lock(&B);  // Execution step 3
	printf("printmsg1\n");
	pthread_mutex_unlock(&B);
	pthread_mutex_unlock(&A);
	return 0;
}

void* printmsg2(void* msg) {
	pthread_mutex_lock(&B);  // Execution step 2
	pthread_mutex_lock(&A);
	printf("printmsg2\n");
	pthread_mutex_unlock(&A);
	pthread_mutex_unlock(&B);
	return 0;
}

int main(int argc, char** argv) {
	pthread_t pt1, pt2;
	pthread_mutex_init(&A, NULL);
	pthread_mutex_init(&B, NULL);
	pthread_create(&pt1, 0, printmsg1, NULL);
	pthread_create(&pt2, 0, printmsg2, NULL);
	pthread_join(pt1, 0);
	pthread_join(pt2, 0);
	pthread_mutex_destroy(&A);
	pthread_mutex_destroy(&B);
	return 0;
}



#include <stdio.h>
#include <pthread.h>
static pthread_mutex_t A, B;
static pthread_cond_t cond;
static int count;

void* f1(void* msg) {
	pthread_mutex_lock(&A);
	pthread_mutex_lock(&B);
	while (count) {
		pthread_cond_wait(&cond, &B);
	}
	pthread_mutex_unlock(&B);
	pthread_mutex_unlock(&A);
	return 0;
}
void* f2(void* msg) {
	pthread_mutex_lock(&A);
	pthread_mutex_lock(&B);
	count--;
	pthread_cond_broadcast(&cond);
	pthread_mutex_unlock(&B);
	pthread_mutex_unlock(&A);
	return 0;
}

int main(int argc, char** argv) {
	pthread_t pt1, pt2;
	pthread_mutex_init(&A, NULL);
	pthread_mutex_init(&B, NULL);
	pthread_cond_init(&cond, NULL);
	count = 1;
	pthread_create(&pt1, 0, f1, NULL);
	pthread_create(&pt2, 0, f2, NULL);

	pthread_join(pt1, NULL);
	pthread_join(pt2, NULL);
	pthread_mutex_destroy(&A);
	pthread_mutex_destroy(&B);
	pthread_cond_destroy(&cond);
	return 0;
}


#include <stdio.h>
#include <pthread.h>
static pthread_mutex_t A, B;

void* printmsg1(void* msg) {
	pthread_mutex_lock(&A);
	pthread_mutex_lock(&B);
	printf("printmsg1\n");
	pthread_mutex_unlock(&B);
	pthread_mutex_unlock(&A);
	return 0;
}

void* printmsg2(void* msg) {
	pthread_mutex_lock(&A);
	pthread_mutex_lock(&B);
	printf("printmsg2\n");
	pthread_mutex_unlock(&B);
	pthread_mutex_unlock(&A);
	return 0;
}

int main(int argc, char** argv) {
	pthread_t pt1, pt2;
	pthread_mutex_init(&A, NULL);
	pthread_mutex_init(&B, NULL);
	pthread_create(&pt1, 0, printmsg1, NULL);
	pthread_create(&pt2, 0, printmsg2, NULL);
	pthread_join(pt1, 0);
	pthread_join(pt2, 0);
	pthread_mutex_destroy(&A);
	pthread_mutex_destroy(&B);
	return 0;
}


#include <stdio.h>
#include <pthread.h>
static pthread_mutex_t A, B;
static pthread_cond_t cond;
static int count;

void* f1(void* msg) {
	pthread_mutex_lock(&B);
	while (count) {
		pthread_cond_wait(&cond, &B);
	}
	pthread_mutex_unlock(&B);
	return 0;
}
void* f2(void* msg) {
	pthread_mutex_lock(&A);
	pthread_mutex_lock(&B);
	count--;
	pthread_cond_broadcast(&cond);
	pthread_mutex_unlock(&B);
	pthread_mutex_unlock(&A);
	return 0;
}

int main(int argc, char** argv) {
	pthread_t pt1, pt2;
	pthread_mutex_init(&A, NULL);
	pthread_mutex_init(&B, NULL);
	pthread_cond_init(&cond, NULL);
	count = 1;
	pthread_create(&pt1, 0, f1, NULL);
	pthread_create(&pt2, 0, f2, NULL);

	pthread_join(pt1, NULL);
	pthread_join(pt2, NULL);
	pthread_mutex_destroy(&A);
	pthread_mutex_destroy(&B);
	pthread_cond_destroy(&cond);
	return 0;
}
}}